//
//  HEHealthRecordViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/26.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEHealthRecordViewController.h"
#import "BalloonView.h"
#import "UIView+AZGradient.h"
#import "HEPersonRecodModel.h"
#import "HEHealthManageViewController.h"
#import "HEHealthAssesViewController.h"
#import "HEPersonDataViewController.h"
#import "HEHealthHisViewController.h"
#import "HECaseListViewController.h"


@interface HEHealthRecordViewController ()<UITableViewDelegate, UITableViewDataSource,BalloonDelegate>

@property(nonatomic,strong) BalloonView *balloonView;
@property(nonatomic,strong) HEPersonRecodModel *personRecodModel;
@property (nonatomic, strong) NSArray *setsArray;
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, assign) BOOL isFrist;

@end

@implementation HEHealthRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"健康档案";
    _isFrist = YES;
    
    if ([[UserManager sharedUserManager].curUserInfo.id isEqualToString: _userModel.id]) {
        self.setsArray = @[
            @{@"name" : @"个人资料", @"icon":@"health_person_info"},
            @{@"name" : @"健康历", @"icon":@"health_history"},
            @{@"name" : @"健康数据", @"icon":@"health_data"},
            @{@"name" : @"健康测评", @"icon":@"health_test"},
            @{@"name" : @"病历资料", @"icon":@"health_person_info"},
        ];
    }else {
        self.setsArray = @[
            @{@"name" : @"个人资料", @"icon":@"health_person_info"},
            @{@"name" : @"健康历", @"icon":@"health_history"},
//            @{@"name" : @"病历资料", @"icon":@"health_person_info"},
        ];
    }
    
    
    
    [self setNav];
    
    [self initTableView];

    [self initUI];
    
    [self loadData];
    [self getAllPhrUserInfo];
    
}

- (void)setNav {
    self.zx_navBar.backgroundColor = KHexColor(@"#3E8BF8");
    self.zx_backBtnImageName = @"back_icon";
    self.zx_navLineView.hidden = YES;
    self.zx_navTintColor = KWhiteColor;
    self.zx_navStatusBarStyle = ZXNavStatusBarStyleLight;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [_balloonView stopSwing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (!_isFrist) {
        [_balloonView startSwing];
    }
    _isFrist = NO;
}

- (void)dealloc {
    [_balloonView stopAnimation];
}

- (void)initUI {
    _balloonView = [[BalloonView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 350) balloonTitles: @[@"无"]];
    _balloonView.backgroundColor = KHexColor(@"4AA4F7");
    [_balloonView az_setGradientBackgroundWithColors:@[KHexColor(@"#3E8BF8"), KHexColor(@"#51B1F6")] locations:nil startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
    _balloonView.delegate = self;
//    [self.view addSubview:_balloonView];
    _tableView.tableHeaderView = _balloonView;
    
    [_tableView bringSubviewToFront:_balloonView];
    
    UILabel *noticeLb = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_balloonView.frame) - 40, KScreenWidth - 20, 40)];
    noticeLb.text = @"档案完成度100%可生成健康画像";
    noticeLb.textColor = KWhiteColor;
    noticeLb.font = DYBlodFont(16);
    noticeLb.textAlignment = NSTextAlignmentRight;
    [_tableView
     addSubview:noticeLb];
}

- (void)initTableView {
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight -  CGRectGetMaxY(_balloonView.frame)) style:UITableViewStyleGrouped];
    _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    _tableView.scrollsToTop = YES;
    _tableView.showsVerticalScrollIndicator = NO;
 
    self.tableView.backgroundColor = KWhiteColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
//    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];

    [self.tableView setSeparatorColor: [UIColor colorWithHexString:@"#EEEEEE"]]; //cell分割线的颜色
}


#pragma mark -
- (void)loadData {
    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue:_userModel.id forKey:@"usid"];
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:Kdzblintegrity parameters:dict success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEObjectDataResponse *response = [HEObjectDataResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            [self.balloonView setValue: response.data];
        }
    } failure:^(NSError * _Nonnull error) {
    }];
}

- (void)getAllPhrUserInfo {
    NSMutableDictionary *dict = @{}.mutableCopy;
//    [dict setValue:_userModel.id forKey:@"usid"];
    [dict setValue:_userModel.pid forKey:@"pid"];

    [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:KgetAllPhrUserInfoById parameters:dict success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        self.personRecodModel = [HEPersonRecodModel mj_objectWithKeyValues:response.data];
        NSMutableArray *arr = @[].mutableCopy;
        for (HEUserDisease *dise in self.personRecodModel.user_disease) {
            [arr addObject:dise.disease_name];
        }
        
        if (arr.count == 0) {
            arr = @[@"无"].mutableCopy;
        }
        
        [self.balloonView configTitles:arr];
        [self.balloonView startBalloonAnimation];

    } failure:^(NSError * _Nonnull error) {
        [self.balloonView startBalloonAnimation];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.setsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.font = DYFont(18);
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *dict = _setsArray[indexPath.row];
    cell.textLabel.text = dict[@"name"];
    cell.imageView.image = IMAGE_NAMED(dict[@"icon"]);

    return cell;
}

#pragma mark -

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 15)];
    v.backgroundColor = CViewBgColor;
    return v;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[UserManager sharedUserManager].curUserInfo.id isEqualToString: _userModel.id]) {
        switch (indexPath.row) {
            case 0:
            {
                HEPersonDataViewController *vc = [HEPersonDataViewController new];
                vc.model = self.personRecodModel;
                vc.userModel = _userModel;
                KWeakSelf
                vc.backBlock = ^{
                    [weakSelf loadData];
                    [weakSelf getAllPhrUserInfo];
                };
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1:
            {
                HEHealthHisViewController *vc = [HEHealthHisViewController new];
                vc.model = self.personRecodModel;
                KWeakSelf
                vc.backBlock = ^{
                    [weakSelf loadData];
                    [weakSelf getAllPhrUserInfo];
                };
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            case 2:
            {
                HEHealthManageViewController *vc = [HEHealthManageViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 3:
            {
                HEHealthAssesViewController *vc = [HEHealthAssesViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 4:
            {
                HECaseListViewController *vc = [HECaseListViewController new];
                vc.usid = _userModel.id;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            default:
                break;
        }
        
    }else {
        switch (indexPath.row) {
            case 0:
            {
                HEPersonDataViewController *vc = [HEPersonDataViewController new];
                vc.model = self.personRecodModel;
                vc.userModel = _userModel;
                KWeakSelf
                vc.backBlock = ^{
                    [weakSelf loadData];
                    [weakSelf getAllPhrUserInfo];
                };
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1:
            {
                HEHealthHisViewController *vc = [HEHealthHisViewController new];
                vc.model = self.personRecodModel;
                KWeakSelf
                vc.backBlock = ^{
                    [weakSelf loadData];
                    [weakSelf getAllPhrUserInfo];
                };
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 2:
            {
                HECaseListViewController *vc = [HECaseListViewController new];
                vc.usid = _userModel.id;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            default:
                break;
        }
    }
}

#pragma mark -
- (NSArray *)setsArray {
    if (!_setsArray) {
        _setsArray = @[
            @{@"name" : @"个人资料", @"icon":@"health_person_info"},
            @{@"name" : @"健康历", @"icon":@"health_history"},
            @{@"name" : @"健康数据", @"icon":@"health_data"},
            @{@"name" : @"健康测评", @"icon":@"health_test"},
        ];

    }
    return _setsArray;
}

@end
